# -*- coding:utf-8 -*-
'''
goldenreportcrawler.py Created on Dec 20, stocklens01

@author: Nick Liang
'''
import sys
sys.path.append(sys.argv[0][:sys.argv[0].rfind('com/stocklens')])

import os
import datetime
import httplib2
from bs4                                   import BeautifulSoup
from time                                  import sleep
from com.stocklens.stock.common.utils      import Logging, TradeUtils

#import sys
#reload(sys)
#sys.setdefaultencoding('gbk')

class GoldenReportCrawler():
    def __init__(self):
        self.link = "http://finance.qq.com/l/stock/baoguangtai/list20141022112531.htm"
        self.emailList = ["elvis@damic.cn", "john.u.yang@ericsson.com", "10455006@qq.com", "nick.liang@ericsson.com", "jackson.u.liu@ericsson.com", "boris_wang2009@qq.com", "15054974@qq.com"]
        #self.emailList = ["nick.liang@ericsson.com"]

    def run(self):
        Logging.LOGGER.debug("Golden Report Crawler Start...")
        doneDateList = dict()
        while True:
            if TradeUtils.isCrawlerTime() == True:
                doneDate = None
                if doneDateList.has_key(str(datetime.date.today())) == False or doneDateList[str(datetime.date.today())] == False:
                    Logging.LOGGER.debug("Try to Crawler new report on Date[%s]", datetime.date.today())
                    doneDate = self.reportCrawler()
                if doneDate is not None:
                    Logging.LOGGER.debug("Successfully Crawler new report on Date[%s]", doneDate)
                    doneDateList[str(doneDate)] = True
            sleep(60)

    def reportCrawler(self):
        doneDate = None
        try:
            h = httplib2.Http(".cache")
            url_link = self.link
            resp, content = h.request(url_link, "GET")

            soup = BeautifulSoup(content)
            #Logging.LOGGER.debug(content)
            #Logging.LOGGER.debug(soup)
            allBlocks = soup.findAll("div",{"class":"mod newslist"})
            #allBlocks = soup.findAll("a",{"target":"_blank"})
            reportLink = ""
            gotReport  = False
            if allBlocks is None: return
            canStockList = list()
            newListBlock = allBlocks[0].findAll("li")
            for block in newListBlock:
                #Logging.LOGGER.debug(block)
                link = block.a.attrs["href"]
                text = block.a.next
                time = block.span.next
                #Logging.LOGGER.debug(link)
                #Logging.LOGGER.debug(text.encode("utf8"))
                #Logging.LOGGER.debug(time.encode("utf8"))
                if text.encode("utf8").startswith("点石成金"):
                    Logging.LOGGER.debug(text.encode("utf8"))
                    Logging.LOGGER.debug(block.span.next.encode("utf8"))
                    reportDateMonth = block.span.next.encode("utf8").split("月")[0]
                    reportDateDay = block.span.next.encode("utf8").split("月")[1].split("日")[0]
                    reportDate = datetime.date(2014,int(reportDateMonth),int(reportDateDay))
                    #Logging.LOGGER.debug("reportDate:[%s]",reportDate)
                    if datetime.date.today() == reportDate:
                        doneDate = reportDate
                        reportLink = link.encode('gb2312')
                        gotReport  = True
                        respReport, contentReport = h.request(link, "GET")
                        soupReport = BeautifulSoup(contentReport)
                        #Logging.LOGGER.debug(soupReport.encode("gbk"))
                        allReportBlocks = soupReport.findAll("div",{"id":"Cnt-Main-Article-QQ"})
                        #Logging.LOGGER.debug(allReportBlocks)
                        if allReportBlocks is None: return
                        reportBlocks = allReportBlocks[0].findAll("p")
                        for report in reportBlocks:
                            reportParts = report.findAll("a")
                            hitFlag = False
                            for rp in reportParts:
                                if rp is None or rp.next is None:
                                    continue
                                #Logging.LOGGER.debug("type(rp.next):%s", type(rp.next)
                                if rp.next.startswith("/keyword"):
                                    #Logging.LOGGER.debug("rp:[%s],rp.href:[%s],astock:[%s]",rp,rp.attrs["href"].encode('gb2312'),str(rp.next))
                                    hitFlag = True

                            if hitFlag == True:
                                for rp in reportParts:
                                    if rp is None: continue
                                    Logging.LOGGER.debug("try stock,rp:[%s],rp.href:[%s],stockname:[%s]", rp,rp.attrs["href"].encode('gb2312'),str(rp.next.encode('utf8')))
                                    if rp is not None and rp.attrs["href"].encode('gb2312').find("sstock/ggcx") > 0:
                                        Logging.LOGGER.debug("hit stock,rp:[%s],rp.href:[%s],stockname:[%s]", rp,rp.attrs["href"].encode('gb2312'),str(rp.next.encode('utf8')))
                                        hList   = rp.attrs["href"].encode('gb2312').split('/')
                                        stockId = hList[len(hList)-1].split('.')[0]
                                        chsStock = dict()
                                        chsStock['link'] = rp.attrs["href"].encode('gb2312')
                                        chsStock['name'] = rp.next.encode("utf8")
                                        chsStock['id']   = stockId
                                        canStockList.append(chsStock)
                        #break

            title = "点石成金"
            text  = "点石成金\n "
            text  = text + "报告链接: %s\n" % reportLink
            for s in canStockList:
                #title = title + "<<股票ID:[%s]，股票名字:[%s]>>" % (s['id'], s['name'])
                #text  = text + "股票ID:[%s]，股票名字:[%s]，股票链接:[%s]\n" % (s['id'], s['name'], s['link'])
                title = title + "<<股票ID:[%s]>>" % (s['id'])
                text  = text + "股票ID:[%s]，股票链接: %s\n" % (s['id'], s['link'])
                Logging.LOGGER.debug("id:%s,name:%s,link:%s", s['id'], s['name'], s['link'])

            if gotReport == True:
                for emailAddress in self.emailList:
                    os.popen('echo "'+text+'" | mail -s "'+title+'" '+emailAddress)

            return doneDate

        except Exception,e:
            Logging.LOGGER.debug("Error %s: %s", e.args[0], e.args[1])
            return doneDate

def crawler_test():
    import logging.config
    logfile = 'conf/stock.logging.cfg'
    logging.config.fileConfig(logfile)
    Logging.LOGGER = logging.getLogger('stock')
    grc = GoldenReportCrawler()
    grc.run()

if __name__== '__main__':
    crawler_test()